﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SPMS.Common;
using SPMS.Data;
using SPMS.Web.Models;
using SPMS.Web.Mvc;

namespace SPMS.Web.Controllers
{
    /// <summary>
    /// 菜单控制器
    /// </summary>
    [AuthorizeEx]    
    public class MenuController : Controller
    {
        /// <summary>
        /// 查询指定角色的菜单数据。
        /// </summary>
        /// <param name="queryModel">查询模型对象。</param>
        /// <returns>
        /// 查询出来的菜单数据。
        /// </returns>
        [HttpGet]
        [JsonHandleError]
        public JsonResult Get(JsonPagingQueryModel queryModel)
        {
            //创建上下文
            using (AccountFileManagementEntities context = new AccountFileManagementEntities())
            {
                //用于保存当前夫节点
                TreeNode parentNode = null;

                //用于保存节点
                List<TreeNode> nodes = new List<TreeNode>();

                //查询数据
                foreach (MenuView item in context.MenuViews.QueryData(queryModel.FilterModel, queryModel.SortModel))
                {
                    //判断分类是否发生变化
                    if (null == parentNode || parentNode.id != item.Category_ID)
                    {
                        //创建父节点
                        parentNode = new TreeNode()
                        {
                            expanded = true,
                            id = item.Category_ID,
                            href = null,
                            data = new List<TreeNode>(),
                            iconCls = item.Category_Icon,
                            leaf = false,
                            parentID = null,
                            text = item.Category_Title,
                            cls = "spms-menu"
                        };

                        //添加到节点集合
                        nodes.Add(parentNode);
                    }

                    //创建子节点
                    TreeNode node = new TreeNode()
                    {
                        data = null,
                        expanded = false,
                        href = "#",
                        iconCls = item.Menu_Icon,
                        id = item.Menu_ID,
                        pageID = item.Page_ID,
                        leaf = true,
                        parentID = item.Category_ID,
                        text = item.Menu_Title,
                        cls = "spms-menu",
                        url = item.Page_Url
                    };

                    //添加到父节点
                    parentNode.data.Add(node);
                }

                //返回菜单数据
                return new JsonResultEx
                (
                    new JsonResultViewModel()
                    {
                        success = true,
                        data = nodes,
                        total = nodes.Count
                    },
                    JsonRequestBehavior.AllowGet
                );
            }
        }
    }
}
